package config;

import cn.bestsign.delta.contact_sam.entity.Contract;
import cn.bestsign.delta.contact_sam.mapper.ContractMapper;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import lombok.val;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.Test;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

public class MybatisPlusConfig {

    public static final String LOCATION_PATTERN = "classpath:mapper/contact_sam/*Mapper.xml";

    public SqlSessionFactory sqlSessionFactory() {

        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        try {
            mybatisSqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(LOCATION_PATTERN));
        } catch (IOException e) {
            e.printStackTrace();
        }
        mybatisSqlSessionFactoryBean.setConfiguration(mybatisConfiguration());
        mybatisSqlSessionFactoryBean.setDataSource(createDataSource());
        Interceptor[] plugins = {new PaginationInterceptor()};
        mybatisSqlSessionFactoryBean.setPlugins(plugins);
        try {
            return mybatisSqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new RuntimeException("nihao");
    }


    public MybatisConfiguration mybatisConfiguration(){
        val dataSource = createDataSource();
        val transactionFactory = new JdbcTransactionFactory();
        val environment = new Environment("Test", transactionFactory, dataSource);
        val configuration = new MybatisConfiguration(environment);
        return configuration;
    }

    public DataSource createDataSource(){
        HikariDataSource hikariDataSource = new HikariDataSource();

//        hikariDataSource.setJdbcUrl("jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'classpath:/h2.init.sql'");
        hikariDataSource.setJdbcUrl("jdbc:mysql://192.168.3.123:3306/contract?useUnicode=true&useSSL=false&characterEncoding=utf8");
//        hikariDataSource.setDriverClassName("org.h2.Driver");
        hikariDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        hikariDataSource.setUsername("root");
        hikariDataSource.setPassword("root123");
        hikariDataSource.setMaximumPoolSize(5);

        return hikariDataSource;
    }


    @Test
    public void tes(){
        ContractMapper mapper = sqlSessionFactory().openSession().getMapper(ContractMapper.class);
        Contract contract = mapper.selectById(1L);
        mapper.selectOne(new QueryWrapper<Contract>().eq("contract_id","0L"));
        System.out.println(contract);
//        mapper.selectPageVo(new Page(1,10));
    }
}
